import json

class Tools:
    def __init__(self):
        self.ProvinceData = {}
        self.SelectProvince = None
    def InitSettings(self):
        DataProvince = None
        #预加载province数据
        with open("WoodlandApp/config/province.json", 'r', encoding='utf-8') as f:
            DataProvince = json.load(f)
        for item in DataProvince:
            self.ProvinceData[item['id']] = [item['province'], item['cities']]

    def PoplarInfo(self, Diameter, Height, quantity):
        # 杨树二元生物量因子模型参数
        c0 = 0.75347
        c1 = 0.25371
        c2 = -0.35596
        biomass = c0 * (Diameter ** c1) * (Height ** c2)
        # 杨树相容性李木材积模型参数
        d0 = 0.00006114
        d1 = 1.89521
        d2 = 0.94759
        volume = d0 * (Diameter ** d1) * (Height ** d2)
        # 计算杨树干材生物量
        dry_biomass = biomass * volume * quantity
        # 杨树干皮、树枝、树叶比例模型
        g1 = 0.64735 * (Height ** -0.36906)
        g2 = 1.13607 * (Diameter ** 0.72356) * (Height ** -1.13102)
        g3 = 1.03548 * (Height ** -1.05634)
        # 计算杨树地上生物量
        above_ground_biomass = dry_biomass * (1 + g1 + g2 + g3)
        # 计算杨树碳库量
        carbon_storage = (dry_biomass * (0.4758 + g1 * 0.4679 + g2 * 0.4723))
        # 杨树二元根茎比模型参数
        e0 = 0.5081
        e1 = 0.1860
        e2 = -0.5065
        rsr = e0 * (Diameter ** e1) * (Height ** e2)
        # 计算杨树地下生物
        below_ground_biomass = dry_biomass * rsr
        # 计算杨树地下碳库
        below_ground_carbon_storage = (biomass * volume * rsr * 0.4644)
        # 计算杨树全树生物量
        total_biomass = above_ground_biomass + below_ground_biomass
        # 计算杨树重复树的生物量
        total_biomass_repeated = total_biomass * quantity
        # 计算杨树全树碳库量
        total_carbon_storage = carbon_storage + below_ground_carbon_storage
        # 计算杨树总碳库量
        total_carbon_storage_all = total_carbon_storage * quantity
        # 计算杨树碳汇
        annual_growth_rate = 0.0316
        carbon_sinks = total_carbon_storage_all * annual_growth_rate
        # 计算杨树总碳汇
        total_carbon_sinks = quantity * carbon_sinks
        # 显示计算结果
        result = {
            'TreeType': '杨树',
            'Diameter': Diameter,
            'Height': Height,
            'Quantity': quantity,
            'dry_biomass': round(dry_biomass, 4),
            'total_biomass': round(total_biomass,4),
            'total_biomass_repeated': round(total_biomass_repeated, 4),
            'carbon_storage': round(carbon_storage, 4),
            'total_carbon_storage_all': round(total_carbon_storage_all, 4),
            'carbon_sinks': round(carbon_sinks, 4),
            'total_carbon_sinks': round(total_carbon_sinks, 4)
                  }
        return result

        # result = (f"树种: 杨树"
        #           # f"地区: {selected_area}\n"
        #           f"树径: {Diameter} cm\n"
        #           f"树高: {Height} cm\n"
        #           f"数量: {quantity}\n"
        #           f"干材生物量: {dry_biomass:.4f} t\n"
        #           f"全树生物量: {total_biomass:.4f} t\n"
        #           f"重复树的生物量: {total_biomass_repeated:.4f} t\n"
        #           f"碳库量: {carbon_storage:.4f} tC\n"
        #           f"总碳库量: {total_carbon_storage_all:.4f} tC\n"
        #           f"碳汇: {carbon_sinks:.4f} tC\n"
        #           f"总碳汇: {total_carbon_sinks:.4f} tC")





